﻿using System;
using System.Globalization;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using StockholmsStad.Web.UI.Class.Controller;
using StockholmsStad.Web.UI.Class.Facade;
using StockholmsStad.Web.UI.Class.Model;
using StockholmsStad.Web.UI.Class.Utilities;

namespace StockholmsStad.Web.UI
{
    /// <summary>
    /// Förstasida
    /// </summary>
    public partial class _Default : Page
    {
        private static readonly DateTimeFormatInfo FORMAT_INFO = new CultureInfo("sv-SE", false).DateTimeFormat;
        private DefaultPageController _controller;

        /// <summary>
        /// Körs när sidan laddas
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpSessionAdapter session = new HttpSessionAdapter(Session);
            WebServiceFacade facade = new WebServiceFacade();
            HttpContextAdapter context = new HttpContextAdapter(Context);
            _controller = new DefaultPageController(new LoginModel(session, facade, context), 
                new BookingModel(session, facade));
            
            if (!Page.IsPostBack)
            {
                _controller.InitializeCachedTimeblocks();
            }
            InitializeTimeBlocks();

            if (_controller.IsUserLoggedIn())
            {
                //Göm länken till Registrering
                Label8.Visible = false;
                hlnRegistration.Visible = false;

                //Göm delen med skicka lösenord
                Label3.Visible = false;
                lblEmailForgotPassword.Visible = false;
                txtEmailForgotPassword.Visible = false;
                bntSendPassword.Visible = false;
                lblEmailForgotPasswordStatus.Visible = false;
            }
        }

        private void InitializeTimeBlocks()
        {
            bdlFreeTimes.Items.Clear();
            IOrderedEnumerable<DateTime> timeblocks = _controller.CachedFreeTimeblocksOrdinary;
            if (timeblocks != null)
            {
                if (timeblocks.Count() == 0)
                {
                    lblNoFreeTimes.Visible = true;
                    lblTenClosestDays.Visible = false;
                    lblFirstFreeTime.Visible = false;
                    lblFirstFreeTimeText.Visible = false;
                    hlkFirstFreeTime.Visible = false;
                }
                else
                {
                    lblNoFreeTimes.Visible = false;
                    lblTenClosestDays.Visible = true;
                    lblFirstFreeTime.Visible = true;
                    lblFirstFreeTimeText.Visible = true;
                    hlkFirstFreeTime.Visible = true;
                    ListItem[] times = DateTimeUtil.GetFirstDatesListItems(10, timeblocks, GetDateTimeLinkText);
                    bdlFreeTimes.Items.AddRange(times);
                    lblFirstFreeTimeText.Text =
                        DateTimeUtil.GetFirstListItems(1, timeblocks, GetDateTimeSingleLinkText)[0].Text;
                }
            }
        }

        private static string GetDateTimeSingleLinkText(DateTime date)
        {
            return date.ToString("dddd yyyy-MM-dd HH:mm", FORMAT_INFO);
        }

        private static string GetDateTimeLinkText(DateTime date)
        {
            return date.ToString("yyyy-MM-dd HH:mm");
        }


        /// <summary>
        /// Körs när användaren har loggat in via login-webcontroll.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Login1_LoggedIn(object sender, EventArgs e)
        {
            if (_controller.HasUnconfirmedBooking)
            {
                Response.Redirect("~/Bokning/Bokning.aspx");
            }
            else
            {
                Response.Redirect("~/VarBokning/VarBokning.aspx");
            }
        }

        /// <summary>
        /// När användaren klickar på 'glömt lösenord'
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bntSendPassword_Click(object sender, EventArgs e)
        {
            string email = txtEmailForgotPassword.Text;
            if (string.IsNullOrEmpty(email))
            {
                lblEmailForgotPasswordStatus.Text = txtEmailForgotPasswordValidator.Text;
            } else
            {
                _controller.SendForgottenPassword(email);
                lblEmailForgotPasswordStatus.Text = "Lösenord har skickats till " + email;
            }
        }
        
        /// <summary>
        /// När användaren klickar på 'Logga ut'
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>        
        protected void LoginStatus1_LoggedOut(object sender, EventArgs e)
        {
            //Abandon Current Session
            HttpSessionAdapter session = new HttpSessionAdapter(Session);
            session.Abandon();
            Session.Abandon();
            Response.Redirect("Default.aspx");
        }
    }
}